


def numIslands(grid):
    if not grid:
        return 0

    numofislands = 0
    rows = len(grid)
    cols = len(grid[0])

    def dfs(r, c):
        # 边界限制
        if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == "0":
            return
        grid[r][c] = "0"
        dfs(r + 1, c)
        dfs(r - 1, c)
        dfs(r, c + 1)
        dfs(r, c - 1)

    for r in range(rows):
        for c in range(cols):
            if grid[r][c] == '1':
                numofislands += 1
                dfs(r, c)

    return numofislands


# 测试用例
grid = [
    ["1", "1", "0", "0", "0"],
    ["1", "1", "0", "0", "0"],
    ["0", "0", "1", "0", "0"],
    ["0", "0", "0", "1", "1"]
]
print(numIslands(grid))
